import { request } from "../../request/index";
import "../../lib/running/runtime";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goods_detail: {},
    previewImage: [],
    // 是否收藏
    isCollection: false,
  },
  id: 0,
  async getGoodsDetail(goods_id) {
    let _res = await request({
      url: "/goods/detail",
      data: { goods_id },
    });
    let { pics, goods_price, goods_name, goods_introduce,goods_small_logo } = _res.data.message;
    let preImg = pics.map((item) => {
      return item.pics_mid;
    });
    this.setData({
      previewImage: preImg,
      goods_detail: {
        goods_small_logo,
        pics,
        goods_price,
        goods_id,
        goods_name,
        goods_introduce: goods_introduce.replace(".webp", ".jpg"),
      },
    });
  },
  // 图片点击事件  //
  handlerImgClick(e) {
    let { previewImage } = this.data,
      { index } = e.currentTarget.dataset;
    wx.previewImage({
      current: previewImage[index],
      urls: previewImage,
    });
  },
  // 收藏商品
  Collection() {
    // 获取当前商品用户
    let { goods_detail } = this.data;
    // 获取缓存用户
    let { state, user = {} } = this.loginState();
    // 用户收藏
    let { collectionList = [] } = user;
    //是否收藏
    let isCollection ;
    if (!state) {
      wx.navigateTo({ url: "/pages/login/login" });
      return null;
    }
    // 搜索是否被收藏
    let goods_index = collectionList.findIndex((Item) => {
      return Item.goods_id === goods_detail.goods_id;
    });
    if (goods_index !== -1) {
      collectionList.splice(goods_index, 1)
      isCollection = false;
    } else {
      collectionList.push(goods_detail);
      isCollection = true;
    }
    this.setData({
      isCollection,
    });
    user.collectionList = collectionList;
    wx.setStorageSync("user", user);
  },
  // 添加购物车
  addShopCar() {
    // 本地购物车
    let cart = wx.getStorageSync("cart") || [];
    // 判断是否存在
    let index = cart.findIndex((Item) => Item.id == this.id);
    // 当商品存在
    if (index !== -1) {
      //  商品数量加一
      cart[index].count += 1;
    } else {
      // 不存在时添加新商品
      let { goods_detail } = this.data;
      let goods = {
        id: this.id,
        count: 1,
        goods_price: goods_detail.goods_price,
        goodsName: goods_detail.goods_name,
        imgUrl: goods_detail.pics[0].pics_mid,
        check: true,
      };
      cart.push(goods);
    }
    wx.setStorageSync("cart", cart);
    wx.showToast({
      title: "成功",
      icon: "success",
      duration: 1500,
      mask: true,
    });
  },
  // 判断用户登录状态
  loginState() {
    const user = wx.getStorageSync("user");
    // 判断登录状态
    if (!user) {
      return { state: false };
    }
    return { state: true, user };
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 食品id
    let goods_id = options.goods_id;
    this.id = goods_id;
    // 发送请求
    this.getGoodsDetail(goods_id);
    let {state,user={}} = this.loginState()
    if(!state) return
    let isCollection = (user.collectionList||[]).some(Item=>{
      return Item.goods_id === goods_id
    })
    this.setData({
      isCollection
    })
  },

});
